from django.shortcuts import render
from django.http import HttpResponse
from . models import *
import os,time

###商品操作视图

#上传图片专用
def pic_upload(myfile):
	#执行上传处理
    filename = str(round(time.time()))+'.'+myfile.name.split('.').pop()

    #打开文件
    destination = open("./static/goods/"+filename,"wb+")
    for chunk in myfile.chunks():      #分块写入文件
       destination.write(chunk)  
    #关闭文件
    destination.close()
    return filename

#获取添加商品页面
def add_goods_page(request):
	typee = Types.objects.extra(select = {'_has':'concat(path,id)'}).order_by('_has')

	for v in typee:
		n = v.path.count(',')-1
		v.name = '... '*n+v.name
	#分配数据
	re_info = {"types":typee}

	return render(request,'dash/goods/add_page.html',re_info)

#添加商品
def add_goods(request):
	#判断是否有商品图片上传
	myfile = request.FILES.get("goods_pic",None)
	# print(myfile)
	if not myfile:
		return HttpResponse('<script>alert("请上传商品图片");location.href="/dashboard/goods/add_goods_page";</script>')
	#执行添加数据
	try:
		goods = Goods()
		goods.typeid = request.POST['pid']
		goods.title  = request.POST['title']
		goods.company = request.POST['company']
		goods.desc = request.POST.get('desc','')
		goods.price = request.POST['price']
		goods.store = request.POST['store']
		goods.photo_url = pic_upload(myfile)
		goods.num = 0
		goods.clicknum = 0
		goods.state = 1

		goods.save()
		#添加成功，跳转商品列表页面
		return HttpResponse('<script>alert("添加成功");location.href="/dashboard/goods/goods_list_page";</script>')
	except:
		return HttpResponse('<script>alert("上传信息有误,请联系开发人员");location.href="/dashboard/goods/add_goods_page";</script>')

#获取商品列表页面
def goods_list_page(request,p_index=1):
	#获取商品信息
	goods = Goods.objects.filter()
	for v in goods :
		v.type_name = Types.objects.get(id=v.typeid).name

	#多条件筛选判断
	if request.GET.get('search_type') == 'title':
		goods = Goods.objects.filter(title__contains = request.GET.get('keywords'))
	elif request.GET.get('search_type') == 'company':
		goods = Goods.objects.filter(company__contains = request.GET.get('keywords'))


	#获取分页数据
	from django.core.paginator import Paginator
	p_index = int(p_index)
	#进行数据分页，实例化对象，设置每页返回数据的条数
	p = Paginator(goods,5)
	#获取分页后的数据
	goods = p.page(p_index)
	#封装返回ref_info
	re_info = {'goods_info':goods}
	return render(request,'dash/goods/list_page.html',re_info)

#删除商品
def del_goods(request):
	try:
		#根据商品id获取商品对象
		goods = Goods.objects.get(id=request.GET['goods_id'])
		#把原来的商品图片删除
		os.remove('./static/goods/'+goods.photo_url)
		goods.delete()
		return HttpResponse('<script>alert("删除成功");location.href="/dashboard/goods/goods_list_page"</script>')
	except:
		return HttpResponse('<script>alert("删除失败");location.href="/dashboard/goods/goods_list_page"</script>')

#访问商品信息修改页面
def goods_edit_page(request):
	#根据商品id获取商品信息对象
	goods = Goods.objects.get(id=request.GET['goods_id'])
	#封装返回数据
	re_info = {'goods_info':goods}
	return render(request,'dash/goods/edit_page.html',re_info)

#执行修改商品信息
def edit_goods(request):
	try:
		#根据商品id获取商品信息对象
		goods = Goods.objects.get(id=request.GET['goods_id'])

		goods.title = request.POST['title']
		goods.price = request.POST['price']
		goods.store = request.POST['store']
		#判断是否有新商品图片上传
		myfile = request.FILES.get("goods_pic",None)

		if myfile:
	    	#删除原来的商品图片
			os.remove('./static/goods/'+goods.photo_url)
	    	#上传获取的新头像
			goods.photo_url = pic_upload(myfile)

		goods.save()

		return HttpResponse('<script>alert("修改信息成功");location.href="/dashboard/goods/goods_list_page"</script>')
	except:
		return HttpResponse('<script>alert("修改信息失败");location.href="/dashboard/goods/goods_list_page"</script>')
		










		



	













